package com.ruoyi.faceApi.thrift;

import com.ylsoft.android.facerepeater.net.thrift.server.transaction.AccessThriftService;
import com.ylsoft.android.iacterminal.net.thrift.server.transaction.AIFTThriftService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class ThriftClient {

	// private ThriftClient client = null;
	private TTransport tTransport = null;
	private TProtocol protocol = null;
	private AIFTThriftService.Client thriftclient = null;

	public static ThriftClient GetInstance() {
		ThriftClient client = new ThriftClient();
		return client;
	}

	public ThriftClient() {

	}

	public AIFTThriftService.Client GetThriftClient(String SERVER_IP, int SERVER_PORT, int TIMEOUT) {
		try {
			// 构建Thrift传输和协议
			if (thriftclient == null) {
				tTransport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
				protocol = new TBinaryProtocol(tTransport);
				thriftclient = new AIFTThriftService.Client(protocol);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return thriftclient;
	}

	public boolean open(String mServerIp,int mThriftPort) {


		try {
			TSocket mSocket = new TSocket(mServerIp, mThriftPort, 5000);
			TFramedTransport mTransport = new TFramedTransport(mSocket);
			TBinaryProtocol mProtocol = new TBinaryProtocol(mTransport);
			AccessThriftService.Client client = new AccessThriftService.Client(mProtocol);
			mTransport.open();
			return true;
		} catch (Exception e) {

			return false;
		}
	}

	public int ClientTransportOpen() {
		if (tTransport != null && !tTransport.isOpen()) {
			try {
				tTransport.open();
				return 0;
			} catch (TTransportException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return -1;
			}
		}
		return -1;
	}

	public void ClientTransportClose() {
		if (tTransport != null) {
			tTransport.close();
		}
	}

	public TTransport gettTransport() {
		return tTransport;
	}

	public void settTransport(TTransport tTransport) {
		this.tTransport = tTransport;
	}

	public TProtocol getProtocol() {
		return protocol;
	}

	public void setProtocol(TProtocol protocol) {
		this.protocol = protocol;
	}

	public AIFTThriftService.Client getThriftclient() {
		return thriftclient;
	}

	public void setThriftclient(AIFTThriftService.Client thriftclient) {
		this.thriftclient = thriftclient;
	}

}
